package cn.com.czpb.client.utils;

import cn.com.czpb.client.api.InterfaceJsonfile;
import cn.com.czpb.client.app.App;
import cn.com.czpb.client.model.base.BaseEntity;
import cn.com.czpb.client.model.news.NewsBean;
import cn.com.czpb.client.net.Factory;

import java.util.HashMap;
import java.util.Map;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by 猪猪侠 on 2017/10/24.
 */

public class StatUtils {
    protected static SPUtil spu;//

    //新闻添加统计量
    public static void addCount(NewsBean nb){
        if ("4".equals(nb.getRtype())){
            LogUtils.i("专题不需要统计");
            return;
        }
        spu = SPUtil.getInstance();
        Map<String, String> map = new HashMap<>();
        map.put("nid", nb.getNid());
        map.put("type", nb.getRtype());
        map.put("siteid", InterfaceJsonfile.SITEID);
        map.put("tid", nb.getTid());
        if (spu.getUser()!=null){
            map.put("uid", spu.getUser().getUid());
        }
        map.put("device",DeviceUtils.getMyUUID(App.getContext()));
        LogUtils.i("addcount-->"+nb.getNid()+"-->"+nb.getRtype()+"-->"+nb.getTid()+"-->"+DeviceUtils.getMyUUID(App.getContext()));
        Factory.provideHttpService()
                .newsAddCount(ParamUtils.getRequestParam(map))
                .subscribeOn(Schedulers.io())
                .filter(new Func1<BaseEntity, Boolean>() {
                    @Override
                    public Boolean call(BaseEntity entity) {
                        if ("200".equals(entity.code)) {
                            return true;
                        }
                        return true;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseEntity>() {
                    @Override
                    public void call(BaseEntity entity) {
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                    }
                });

    }
}
